/*
 * HttpFileBody.cpp
 *
 */

#include "HttpFileBody.h"
#include "HttpMessage.h"
#include "HttpHeader.h"
#include <sstream>

HttpFileBody::HttpFileBody(const std::string& filename) {
	file.open(filename.c_str());
	if (!file.is_open())
		throw MessageException("File not found");
	file.seekg(0, std::fstream::end);
	if(file.tellg() == -1)
		throw MessageException("Invalid file size");
	size = file.tellg();
	file.seekg(0, std::fstream::beg);
}

HttpFileBody::~HttpFileBody() {
	if (file.is_open())
		file.close();
}

HttpHeader* HttpFileBody::getContentLength() const {
	std::stringstream buffer;
	buffer << size;
	return new HttpHeader(HttpHeader::CONTENT_LENGTH, buffer.str());
}

size_t HttpFileBody::getSize() const {
	return size;
}

std::istream& HttpFileBody::getStream() {
	return file;
}
